/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCategoryManager.java
 *
 * Created on 11-03-2011, 00:18:41
 */

package GUI;

import interact.DataInteraction;
import interact.ValidateData;
import interact.connect;
import java.util.Vector;

/**
 *
 * @author UnderGround
 */
public class frmCategoryManager extends javax.swing.JPanel {

    /** Creates new form frmCategoryManager */
    public frmCategoryManager() {
        initComponents();
        connect.loadDataTable3("getCategoryToTable", tblCategory);
    }
    
    public static void loadDataTable(){
        connect.loadDataTable3("getCategoryToTable", tblCategory);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCategory = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jPanel1 = new javax.swing.JPanel();
        btnDel = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        txtCategory = new javax.swing.JTextField();
        lblCategory = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jDesktopPane1.setBackground(new java.awt.Color(51, 51, 51));

        tblCategory.setBackground(new java.awt.Color(255, 255, 204));
        tblCategory.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(197, 231, 31), new java.awt.Color(212, 40, 40), new java.awt.Color(223, 27, 40), new java.awt.Color(23, 212, 87)));
        tblCategory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblCategory.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblCategory);

        jScrollPane1.setBounds(0, 0, 710, 370);
        jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(16, 173, 40), new java.awt.Color(194, 70, 70), new java.awt.Color(238, 149, 23), new java.awt.Color(195, 231, 21)));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/del.png"))); // NOI18N
        btnDel.setText("delete");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });
        jPanel1.add(btnDel, new org.netbeans.lib.awtextra.AbsoluteConstraints(415, 62, 90, -1));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/up.png"))); // NOI18N
        btnUpdate.setText("update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(297, 62, 100, -1));
        jPanel1.add(txtCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(288, 26, 166, -1));

        lblCategory.setText("Category");
        jPanel1.add(lblCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(208, 29, 70, -1));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/addcategory.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(189, 62, 90, -1));

        jPanel1.setBounds(0, 370, 710, 140);
        jDesktopPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 710, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        CategoryName = txtCategory.getText();
        System.out.println(ValidateData.checkDuplicateData(CategoryName, "checkDuplicate_category", "count"));
        if(!ValidateData.checkEmpty(CategoryName, txtCategory)){
            ValidateData.showMessage("Category invalid. Please enter again!");
            return;
        }else if(!ValidateData.checkDuplicateData(CategoryName, "checkDuplicate_category", "count")){
            ValidateData.showMessage("duplicate category");
            return;
        }else{
            vt = new Vector();
           vt.add(CategoryName);
           DataInteraction.updateData("addCategory", vt);
           loadDataTable();
           ValidateData.showMessage("add category successful");
        }
         
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        rowSelected = tblCategory.getSelectedRow();
        String category = txtCategory.getText().trim();
        if(rowSelected == -1){
            ValidateData.showMessage("Please choose category to update");
            return;
        }else if(!ValidateData.checkDuplicateData(CategoryName, "checkDuplicate_category", "count")){
            ValidateData.showMessage("duplicate category");
            return;
        }else if(!ValidateData.checkEmpty(category, txtCategory)){
            ValidateData.showMessage("Category invalid. Please enter again");
            return;
        }else{
        id =(String) tblCategory.getValueAt(rowSelected, 0);
        CategoryName = txtCategory.getText();
        vt = new Vector();
        vt.add(id);
        vt.add(CategoryName);
        DataInteraction.updateData("updateCategory", vt);
        loadDataTable();
         ValidateData.showMessage("update successful");
        }
       
        
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        rowSelected = tblCategory.getSelectedRow();
       
        if (rowSelected == -1) {
            ValidateData.showMessage("please choose a category to delete");
            return;
        }
        id = (String) tblCategory.getValueAt(rowSelected, 0);
         if (ValidateData.check_Status_To_Del("check_del_category", id)) {
            ValidateData.showMessage("You can't delete this category");
            return;
        } else {
        
            DataInteraction.deleteRecord("delCategory", id);
            loadDataTable();
            ValidateData.showMessage("delete category successful");
        }
    }//GEN-LAST:event_btnDelActionPerformed

    private Vector vt;
    private int rowSelected;
    private String id,CategoryName;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCategory;
    private static javax.swing.JTable tblCategory;
    private javax.swing.JTextField txtCategory;
    // End of variables declaration//GEN-END:variables

}
